﻿using Furion.JsonSerialization;
using iemtm.WeChat.Core.Extension;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace iemtm.WeChat.Core.Util
{
    /// <summary>
    /// 与TCP、通讯封装
    /// </summary>
    public class CommLocal
    {
        public static async  Task<List<CodeValuePair>> ReadParas( List<CodeValuePair> prs, string dtuId, int responseTimeout = 7)
        {
            Dictionary<string, CodeValuePair> prsUpper = (from ii in prs select ii).ToDictionary(kk => { return kk.code.ToUpper(); });
            List<string> prsLower = (from ii in prs select ii.code).ToList();
            string dtuCmd = (from ii in prs select ii.code).ToList().StrListToStr(";");
            if (dtuCmd != "")
            {
                string Tmp =await TcpCommunication.ReadParas(dtuCmd, dtuId, responseTimeout);
                CmdReportModel cm = JSON.GetJsonSerializer().Deserialize<CmdReportModel>(Tmp);
                if (!string.IsNullOrEmpty(cm.receivedStr) && cm.receivedStr != "ERR DATA" )
                {
                    string[] cfgCodes = cm.receivedStr.Split(";".ToCharArray());

                    if (cfgCodes.Length >= 2)
                    {
                        List<CodeValuePair> rPairs = new List<CodeValuePair>();
                        for (int i = 0; i < cfgCodes.Length; i += 2)
                        {
                            string ppName = cfgCodes[i];
                            string ppValue = null;
                            if (i + 1 < cfgCodes.Length)
                            {
                                ppValue = cfgCodes[i + 1];
                            }
                            else
                                break;
                            if (ppValue == null)
                                ppValue = "";
                            if (ppName != null && ppName != "" && (prsUpper.ContainsKey(ppName) || prsLower.Contains(ppName)))
                            {
                                CodeValuePair np = new CodeValuePair();
                                np.code = prsUpper[ppName.ToUpper()].code;
                                np.value = gbItems.Contains(np.code) ? ppValue.ToGB2312() : ppValue;
                                rPairs.Add(np);
                            }
                        }
                        return rPairs;
                    }
                }
            }
            return null;
        }

  
        /// <summary>
        /// 判读 采集器是不是 团成
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsTuangChen(string str)
        {
            for (int i = 0; i < str.Length; i++)
            {
                if (!Char.IsNumber(str, i))
                    return false;
            }
            return true;
        }
        public static List<string> gbItems = new List<string>()
        {
            "GensetArea".ToUpper(),
            "GensetNum".ToUpper(),
            "Extension".ToUpper(),
            "Dateclock".ToUpper(),
            "GensetArea",
            "GensetNum",
            "Extension",
            "Dateclock",
            "RunningPeriod".ToUpper(),
            "KWpower".ToUpper(),
            "GenAlarm".ToUpper()
        };

    }

    /// <summary>
    /// 命令记录模型
    /// </summary>
    public class CmdReportModel
    {
        public string sendStr { get; set; }
        public DateTime sendDate { get; set; }

        public string receivedStr { get; set; }
        public DateTime receivedDate { get; set; }
        public string Msg { get; set; } = "无返回";

    }


    public enum ECmd660Result
    {
        Returned = 1,
        Timeout = 2,
        LinkBreak = 3
    }
}
